<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : WechatAppAddonsConfig.php
 * Created on : 2021-2-26 9:46:03
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace app\wechat\model;

use think\Model;
use haoteam\service\WechatAppService;

/**
 * 小程序配置模型
 * Class WechatAppAddonsConfig
 * Tiger <1192851302@qq.com> 2021-2-26 9:46:03
 */
class WechatAppAddonsConfig extends Model {

    /**
     * 保存配置
     * @authr Tiger <1192851302@qq.com>
     * @param type $configs
     * @return type
     */
    public static function preserve($configs) {
        $addon = WechatAppService::instance()->addonInfo['addon'];
        $mid = WechatAppService::instance()->mid;
        $config = json_encode($configs);
        if (self::where(compact('mid', 'addon'))->count()) {
            return self::update(compact('mid', 'addon', 'config'), compact('mid', 'addon'));
        }
        return self::create(compact('mid', 'addon', 'config'));
    }

    /**
     * 获取全部配置
     * @authr Tiger <1192851302@qq.com>
     * @return type
     */
    public static function getAll() {
        $appInfo = WechatAppService::instance()->addonInfo;
        $mid = WechatAppService::instance()->mid;
        $config = json_decode(self::where(['mid' => $mid, 'addon' => $appInfo['addon']])->value('config'), true);
        $data = [];
        foreach ($appInfo['config'] as $key => $item) {
            if (isset($config[$item['name']])) {
                $item['value'] = $config[$item['name']];
            }
            $data[] = $item;
        }
        return $data;
    }

}
